var fs = require('fs')
const finmae = './data.json'


// 获取全部数据 
const getAllData = () => {
    // 尝试执行一段代码没问题就执行try里的，有问题就执行catc里的没问题就执行try里的
    try {
        let all = fs.readFileSync(finmae)
        all = all.toString()
        return JSON.parse(all)
    } catch (e) {
        if (e.errno === -4058) {
            fs.writeFileSync(finmae, '{}')
            return {}
        }
    }
}
// console.log(getAllData());


// 获取数据
const getbooklist = () => {
    let all_data = getAllData()
    if (!all_data.book) {
        return []
    }
    return all_data.book
}
// console.log(getbooklist());


// 导出去
module.exports = {
    getbooklist,
}